package BS;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main{
        public static void main(String[] args) {
                Scanner scanner = new Scanner(System.in);

                // 用户列表
                List<User> userList = new ArrayList<>();

                // 逐行输入所有用户开户信息
                while (true) {
                        String nextLine = scanner.nextLine();
                        if (nextLine.equals("end")) break;

                        String[] split = nextLine.split(" ");
                        if (split.length != 2 || !split[0].startsWith("u") || split[1].equals("1")) continue;

                        String number = split[0].substring(2);
                        UserRecords userRecords = new UserRecords();
                        ChargeMode chargeMode = new NanchangChargeMode();
                        userList.add(new User(userRecords, chargeMode, number));
                }

                // 逐行输入所有通讯信息
                while (true) {
                        String nextLine = scanner.nextLine();
                        if (nextLine.equals("end")) break;

                        String[] split = nextLine.split(" ");
                        if (split.length != 5 || !split[0].startsWith("t")) continue;

                        String callingNumber = split[1];
                        String answerNumber = split[2];
                        String startTime = split[3] + " " + split[4];
                        String endTime = scanner.nextLine().split(" ")[4]; // 结束时间在下一行
                        CallRecord callRecord = new CallRecord(callingNumber, answerNumber, startTime, endTime);

                        // 寻找通话双方的用户记录
                        User callingUser = null;
                        User answerUser = null;
                        for (User user : userList) {
                                if (user.getNumber().equals(callRecord.callingNumber)) {
                                        callingUser = user;
                                }
                                if (user.getNumber().equals(callRecord.answerNumber)) {
                                        answerUser = user;
                                }
                                if (callingUser != null && answerUser != null) break; // 如果已找到通话双方的用户记录，跳出循环
                        }

                        // 更新通话双方的用户记录
                        if (callingUser != null) {
                                callingUser.userRecords.localCallingRecords.add(callRecord);
                        }
                        if (answerUser != null) {
                                answerUser.userRecords.localAnswerRecords.add(callRecord);
                        }
                }

                // 计算当月费用并输出
                Collections.sort(userList, Comparator.comparing(User::getNumber)); // 按号码字符从小到大排序
                for (User user : userList) {
                        double totalCost = user.getTotalCost();
                        user.updateBalance(totalCost);
                        System.out.printf("%s %.2f %.2f\n", user.number, totalCost, user.balance);
                }
        }
}
// 南昌市电信分公司的计费方式
 class NanchangChargeMode extends ChargeMode {
        public static final double LOCAL_CALLING_COST = 0.1; // 市内拨打电话费用（元/分钟）
        public static final double PROVINCIAL_CALLING_COST = 0.3; // 省内拨打电话费用（元/分钟）
        public static final double NATIONAL_CALLING_COST = 0.6; // 国内拨打电话费用（元/分钟）

        public NanchangChargeMode() {
                super(20, new HashMap<>());
                chargeRules.put("localCalling", LOCAL_CALLING_COST);
                chargeRules.put("provincialCalling", PROVINCIAL_CALLING_COST);
                chargeRules.put("nationalCalling", NATIONAL_CALLING_COST);
        }

        // 计算当月的费用
        public double calculate(UserRecords userRecords) {
                double totalCost = 0;
                List<CallRecord> allCallRecords = userRecords.getAllCallRecords();
                for (CallRecord callRecord : allCallRecords) {
                        String areaCode = callRecord.callingNumber.substring(0, 4);
                        String costKey;
                        if (areaCode.equals("0791")) { // 市内拨打电话
                                costKey = "localCalling";
                        } else if (areaCode.compareTo("0790") >= 0 && areaCode.compareTo("0799") <= 0
                                || areaCode.equals("0701")) { // 省内拨打电话
                                costKey = "provincialCalling";
                        } else { // 国内拨打电话
                                costKey = "nationalCalling";
                        }
                        double cost = chargeRules.get(costKey) * Math.ceil(callRecord.getDuration()); // 计算费用，向上取整
                        totalCost += cost;
                }
                totalCost += monthlyRent; // 加上月租

                return totalCost;
        }
}

// 通话记录类
 class CallRecord extends CommunicationRecord {
        Date startTime; // 通话开始时间
        Date endTime; // 通话结束时间
        String callingAddressAreaCode; // 拨号地点的区号
        String answerAddressAreaCode; // 接听地点的区号

        public CallRecord(String callingNumber, String answerNumber, String startTime, String endTime) {
                super(callingNumber, answerNumber);
                this.callingAddressAreaCode = callingNumber.substring(0, 4);
                this.answerAddressAreaCode = answerNumber.substring(0, 4);
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                try {
                        this.startTime = dateFormat.parse(startTime);
                        this.endTime = dateFormat.parse(endTime);
                } catch (ParseException e) {
                        e.printStackTrace();
                }
        }

        // 获取通话开始时间
        public Date getStartTime() {
                return startTime;
        }

        // 获取通话结束时间
        public Date getEndTime() {
                return endTime;
        }
}

// 抽象的通讯记录类
abstract  class CommunicationRecord {
        String callingNumber; // 拨打号码
        String answerNumber; // 接听号码

        public CommunicationRecord(String callingNumber, String answerNumber) {
                this.callingNumber = callingNumber;
                this.answerNumber = answerNumber;
        }

        // 获取拨打号码
        public String getCallingNumber() {
                return callingNumber;
        }

        // 获取接听号码
        public String getAnswerNumber() {
                return answerNumber;
        }

        // 获取该通讯记录的时长（以分为单位）
        public long getDuration() {
                return getEndTime().getTime() / 60000 - getStartTime().getTime() / 60000;
        }

        // 获取通话开始时间
        public abstract Date getStartTime();

        // 获取通话结束时间
        public abstract Date getEndTime();
}

// 用户类，包括用户记录、余额、计费方式和号码
 class User {
        UserRecords userRecords;
        double balance;
        ChargeMode chargeMode;
        String number;

        public User(UserRecords userRecords, ChargeMode chargeMode, String number) {
                this.userRecords = userRecords;
                this.balance = 100.00; // 初始余额为100元
                this.chargeMode = chargeMode;
                this.number = number;
        }

        // 获取号码
        public String getNumber() {
                return number;
        }

        // 获取当月总费用
        public double getTotalCost() {
                return chargeMode.calculate(userRecords);
        }

        // 获取余额
        public double getBalance() {
                return balance;
        }

        // 更新余额
        public void updateBalance(double cost) {
                balance -= cost;
        }
}

// 计费方式的抽象类
abstract  class ChargeMode {
        Map<String, Double> chargeRules; // 计费规则的集合
        double monthlyRent; // 月租

        public ChargeMode(double monthlyRent, Map<String, Double> chargeRules) {
                this.monthlyRent = monthlyRent;
                this.chargeRules = chargeRules;
        }

        // 获取月租
        public double getMonthlyRent() {
                return monthlyRent;
        }

        // 计算当月的费用
        public abstract double calculate(UserRecords userRecords);
}

// 用户记录类
 class UserRecords {
        List<CallRecord> localCallingRecords; // 市内通话记录
        List<CallRecord> provincialCallingRecords; // 省内通话记录
        List<CallRecord> nationalCallingRecords; // 国内通话记录
        List<CallRecord> localAnswerRecords; // 市内接听记录
        List<CallRecord> provincialAnswerRecords; // 省内接听记录
        List<CallRecord> nationalAnswerRecords; // 国内接听记录

        public UserRecords() {
                localCallingRecords = new ArrayList<>();
                provincialCallingRecords = new ArrayList<>();
                nationalCallingRecords = new ArrayList<>();
                localAnswerRecords = new ArrayList<>();
                provincialAnswerRecords = new ArrayList<>();
                nationalAnswerRecords = new ArrayList<>();
        }

        // 获取所有通话记录
                List<CallRecord> getAllCallRecords() {
                List<CallRecord> records = new ArrayList<>();
                records.addAll(localCallingRecords);
                records.addAll(provincialCallingRecords);
                records.addAll(nationalCallingRecords);
                records.addAll(localAnswerRecords);
                records.addAll(provincialAnswerRecords);
                records.addAll(nationalAnswerRecords);
                return records;
        }
}


